/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dataBasePlaylisted;

import controlPlaylisted.ControlMusicCommand;
import Response.ResponseMusic;
import Request.RequestMusic;
import dataPlaylisted.Music;

import java.beans.XMLEncoder;

import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import java.util.ArrayList;

/**
 *
 * @author Administrador
 */
public class setMusicListCommand implements ControlMusicCommand{




    public setMusicListCommand() throws FileNotFoundException, IOException, ClassNotFoundException{

    }

//    public void execute(RequestUser _req, ResponseUser _resp) {
//
//        ArrayList<User> list;
//
//        list = _req.getObjectList();
//
//        try{
//            UserSerializable s_users;
//            s_users = UserSerializable.getInstance();
//
//            s_users.setUserList(list);
//
//            m_out.writeObject(s_users);
//            m_out.close();
//        }
//        catch(IOException ex){
//        }
//    }


    public void execute(RequestMusic _req, ResponseMusic _resp) {

        String file_name = "MusicModel.xml";
        FileOutputStream m_fos = null;
        ObjectOutputStream m_out = null;
        XMLEncoder m_xenc;

        try{
            m_fos = new FileOutputStream(file_name);
        }catch(IOException e){
        }

//        try{
//            m_out = new ObjectOutputStream(m_fos);
//        }catch(IOException e){
//        }

        m_xenc = new XMLEncoder(m_fos);

        ArrayList<Music> list;
        //Dados usuarios
        list = _req.getObjectList();

        ArrayList<String> str_list;
        str_list = new ArrayList<String>();

        for(int i = 0; i < list.size(); i++){
            str_list.add(list.get(i).getTrackName());
            str_list.add(list.get(i).getArtistName());
            str_list.add(list.get(i).getSyle());
            str_list.add(list.get(i).getCammelot());
        }

        // Write object.
        m_xenc.writeObject(str_list);

	m_xenc.flush();
	m_xenc.close();
	try{
            m_fos.close();
        }catch(IOException e){
        }
    }

}

